<?php
FLEA::loadClass('Controller_Base');
FLEA::loadClass('FLEA_Helper_Pager');
FLEA::loadFile('FLEA_Ajax_JSON');

class Controller_UserAdmin extends Controller_Base {
	var $_modelUsers;
	function Controller_UserAdmin() {
		$this->_modelUsers = & FLEA::getSingleton("Model_Users");
	}
	function actionIndex() {
		$users = $this->_modelUsers->findAll();
		require(TPL_DIR."/usersmanage.php");
	}
	function actionCreateNew() {
		$num = 10;
		$newuser = array(
			"password"=>"801030",
		);
		for($i=0;$i<$num;$i++) {
			$newuser['username'] = "jiangsujier".$i;
			$newuser['email'] = "jiangsujier".$i."@163.com";
//			dump($newuser);
			$this->_modelUsers->create($newuser);
		}
	}
	function actionUserList() {
//		$users = $this->_modelUsers->findAll();	//
		$page = $_POST["page"];
		$pageSize =$_POST["pageSize"];
		$pageSize = $pageSize ==''?50:$pageSize;
		$pager =& new FLEA_Helper_Pager($this->_modelUsers, $page, $pageSize, $conditions, $sort);
//		$pager->renderPageJumper();
		$users = $pager->findAll();
		$column = array('email','disable');
		echo datagrid_tbl($column,$users);
//		echo $pager->totalCount;
//		echo $output;
//		require(TPL_DIR."/usersmanage.php");
//		dump($users);
	}
	function actionDelUser() {
		$this->_modelUsers->removeByConditions("uid in (".$_POST['id'].")");
	}
	function actionDisableUser() {
		$this->_modelUsers->updateField("uid in(".$_POST['id'].")","disable",1);
	}
	function actionActiveUser() {
		$this->_modelUsers->updateField("uid in(".$_POST['id'].")","disable",0);
	}

}

//要得到的字段,显示字段
function translate_email($val) {
	return $val;
}
function translate_disable($val) {
	return $val == '1'?"<input type='checkbox' checked='checked' />":"<input type='checkbox' />";
}
function datagrid_tbl($col,$dataarr) {
	$code ="<form name='form1'><table id='datagrid'>";
	$code .="<thead>";
	$code .="<tr>";
	$code .="<td><input type='checkbox' name='checkall' id='checkall_h' /></td>";
	foreach($col as $cname) {
		$code .="<td>".$cname."</td>";
	}
	$code .="</tr>";
	$code .="</thead>";
	$code .="<tfoot>";
	$code .="<tr>";
	$code .="<td><input type='checkbox' name='checkall' id='checkall_f' /></td>";
	foreach($col as $cname) {
		$code .="<td>".$cname."</td>";
	}
	$code .="</tr>";
	$code .="</tfoot>";
	$code .="<tbody>";
	foreach($dataarr as $record) {
		$code .="<tr>";
		$code .="<td><input type='checkbox' name='checkall_' value='".$record['uid']."' /></td>";
		foreach($record as $key=>$value) {
			if(in_array($key,$col)) {
				$func = "translate_".$key;
				$code .="<td>".$func($value)."</td>";
			}
		}
		$code .="</tr>";
	}
	$code .="</tbody>";
	$code .="</table></form>";
	$code .="
	<select name='act' id='act_user' onchange='act_user(this.value);'>
		<option value='0'>请选择要执行的操作</option>
		<option value='Del'>删除所选的用户</option>
		<option value='Active'>激活所选的用户</option>
		<option value='Disable'>冻结所选的用户</option>
	</select>
	";
	return $code;
}
function datagrid($col,$dataarr) {
	$code ="";
	$code .="<ul id='datagrid'>";
	$code .="";	//表头
	foreach($dataarr as $record) {
	$code .="<li>";
	$code .="<ul>";
	$code .="<li class='data_id'><input type='checkbox' name='id' /></li>";
		foreach($record as $key => $value) {
			if(in_array($key,$col)) {
				$code .="<li class='data_".$key."'>".$value."</li>";
			}
		}
	$code .="</ul>";
	$code .="</li>";
	}
	$code .="</ul>";
	return $code;
}
?>